package com.sxz.sbs.module.staff;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.IntentConstant;
import com.sxz.sbs.constant.TypeConstant;
import com.sxz.sbs.event.OperateSuccessEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.AllHousesAdapter;
import com.sxz.sbs.module.client.bean.AllHousesBean;
import com.sxz.sbs.module.staff.mode.StaffIntentBean;
import com.sxz.sbs.module.staff.presenter.AllHousesPresenter;
import com.sxz.sbs.module.staff.view.AllHousesView;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.widget.CommonTitle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 员工管理 -- 案场选择
 */
@Route(path = RouterUrl.STAFF_HANDLE)
public class FieldSelectActivity extends BaseActivity<AllHousesPresenter> implements AllHousesView {
    @Autowired
    String status;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private AllHousesAdapter adapter;
    private List<AllHousesBean.HousesBean> dataList = new ArrayList<>();
    private int currentPage = 1;
    private String keyWords = "";
    private String fromTag = "2";
    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        ARouter.getInstance().inject(this);
        //  .showLeftReadyImg(R.drawable.ic_search_left)
        new CommonTitle.Builder(this).setTitle("选择案场").build().show();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getHousesData();
            }
        });
        refreshLayout.setRefreshing(true);
        getHousesData();
        adapter = new AllHousesAdapter(dataList);
        recyclerView.setAdapter(adapter);
        try {
            fromTag = status.split("=")[1];
        } catch (Exception e) {
            fromTag = "2";
        }

        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                StaffIntentBean bean = new StaffIntentBean();
                bean.setParkingId(dataList.get(position).getId());
                bean.setParkingName(dataList.get(position).getName());
                bean.setTag(fromTag);
                switch (fromTag) {
                    // 案场内调换
                    case "2":
                        bean.setDisposeType(TypeConstant.STAFF_MANGER_SWAP);
                        startActivity(new Intent(FieldSelectActivity.this,StaffDisposeActivity.class)
                                .putExtra(IntentConstant.STAFF_INTENT_BEAN, bean));
                        break;
                    // 案场外调换
                    case "3":
                        bean.setDisposeType(TypeConstant.STAFF_MANGER_SWAP_OUT);
                        startActivity(new Intent(FieldSelectActivity.this,StaffDisposeActivity.class)
                                .putExtra(IntentConstant.STAFF_INTENT_BEAN, bean));
                        break;
                    // 员工离职
                    case "6":
                        bean.setDisposeType(TypeConstant.STAFF_MANGER_DIMISSION);
                        startActivity(new Intent(FieldSelectActivity.this,StaffDisposeActivity.class)
                                .putExtra(IntentConstant.STAFF_INTENT_BEAN, bean));
                        break;
                    // 添加员工
                    case "1234":
                        Intent intent = new Intent();
                        intent.putExtra("parkId",dataList.get(position).getId());
                        intent.putExtra("parkName",dataList.get(position).getName());
                        setResult(1006,intent);
                        finish();
                        break;
                    // 计算小区
                    case "amortize":
                        Intent intent1 = new Intent();
                        intent1.putExtra("parkId",dataList.get(position).getId());
                        intent1.putExtra("parkName",dataList.get(position).getName());
                        setResult(1007,intent1);
                        finish();
                        break;
                    default:
                        break;
                }
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getHousesData();
            }
        },recyclerView);
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getHousesData();
            }
        });
    }

    /**
     * 获取楼盘列表
     */
    private void getHousesData () {
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("keyWords", keyWords);
        map.put("pageNumber", currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        mPresenter.getAllHouses(map,false);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_field_select;
    }

    @Override
    protected AllHousesPresenter createPresenter() {
        return new AllHousesPresenter();
    }


    @OnClick(R.id.iv_title_left_img)
    public void onViewClicked() {
        ToastUtils.showShort("搜索");
    }


    @Override
    public void onGetHousesDataSuccess(AllHousesBean bean) {
        if (mIsLive && bean != null) {
            refreshLayout.setRefreshing(false);
            if (bean.getList() != null && bean.getList().size() >0 ) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    dataList.clear();
                    dataList.addAll(bean.getList());
                    adapter.notifyDataSetChanged();
                } else {
                    dataList.addAll(bean.getList());
                    adapter.notifyDataSetChanged();
                }
                adapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    dataList.clear();
                    adapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    if (dataList.size() > AppConstant.PAGE_SIZE) {
                        adapter.loadMoreEnd();
                    } else {
                        adapter.loadMoreEnd(true);
                    }
                }
            }

        }
    }

    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
        adapter.loadMoreFail();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOperateSuccessEvent(OperateSuccessEvent event) {
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
